<?php

namespace App\Http\Controllers\home;

use App\Model\book;
use App\Model\coller;
use App\Model\feedback;
use App\Model\gomoney;
use App\Model\order;
use App\Model\readuser;
use App\Model\record;
use App\Model\tofeedback;
use App\Tool\SMS\SendTemplateSMS;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class PersonController extends Controller
{
    public function goin(Request $request)
    {
        if (session('code')==$request->code){

            $books='';
            return view('linfeng.home.change_pwd',compact('books'));
        }else{
            $request->session()->flash('message','验证码不正确或者不存在!');
            return redirect('home/person_edit');
        }
    }

    public function newpwd(Request $request)
    {
        $rules = array(
            'pwd' => 'required',
            'newpassword' => 'required',
            'newpassword_old' => 'required',
        );
        $mess = array(
            'pwd.required' => '手机号码不能为空',
            'newpassword.required' => '密码不能为空',
            'newpassword_old.required' => '短信验证码不能为空',
        );
        $this->validate($request, $rules, $mess);
        $newpwd = $request->pwd;
        if ($request->newpassword == $request->newpassword_old) {
            $update = readuser::where('id', session('readid'))->where('pwd', $newpwd)->get();
            if (count($update)) {
                $add = readuser::find(session('readid'));
                $add->pwd = $request->newpassword;
                $add->save();
//                dd(1);
                $request->session()->flash('success','1');
                $read=readuser::find(session('readid'));

                return view('linfeng.home.person_edit'.compact('read'));
//                return redirect('home/person_edit');
            } else {
//                dd(2);
                $request->session()->flash('succass','旧密码错误');
                return back();
            }
        }else{
//            dd(3);
            $request->session()->flash('success','俩次新密码不相同');
            return back();
        }

    }

    //修改手机验证码
    //注册发送手机验证码
    public function PersonSMS(Request $request)
    {
        $no=readuser::where('tel',$_GET['tel'])->get();
        if (count($no)){
            return 111;
        }else{
        $tel=$_GET['tel'];
        session(['tel'=>$tel]);
        $rand=rand(1000,9999);
        session(['code'=>$rand]);
        $sms=new SendTemplateSMS();
        $sms->sendSMS($tel,array($rand,5),1);
        return 333;
        }
    }

    public function newtel(Request $request){
         $newtel=$request->newtel;
//         dd($newtel);
         if($request->code==session('code')){
             $up=readuser::find(session('readid'));
             $up->tel=$newtel;
             $up->save();
             $ok='';
             return redirect('home/person_edit')->with('修改成功',$ok);
         }else{
             $no='';
             return redirect('home/person_edit')->with('修改失败',$no);
         }

    }

    //修改头像
    public function userIcon(Request $request)
    {
        if ($request->isMethod('post')){
            //新设计的文件名并把旧的文件删除
            $name=session('readid').'_'.time().'.jpg';
            $upicon=readuser::find(session('readid'));
            $oldicon=$upicon->icon;
            if (!empty($oldicon)){
            if (file_exists(public_path().'/image/home/'.$oldicon)){
                unlink(public_path().'/image/home/'.$oldicon);
            }
            }
            //获取新文件名并上传
            $upicon=readuser::find(session('readid'));
            $upicon->icon=$name;
            $upicon->save();

            $dir=public_path().'/image/home';
            $ok=$request->file0->move($dir,$name);
//            dd($name);
            return redirect('home/Person/userIcon')->with('goodicon','成功');
        }
        $read=readuser::find(session('readid'));
        $books='';
        return view('linfeng.home.userIcon',compact('books','read'));
    }
//    //头像确认上传
//    public function readiconup(Request $request)
//    {
//        //新设计的文件名
//        $name=session('readid').'_'.time().'.jpg';
//
//        if (file_exists(public_path().'/img/home'.session('readid').'.jpg')){
//            unlink(public_path().'/img/home'.session('readid').'.jpg');
//        }
//
//        $upicon=readuser::find(session('readid'));
//        $upicon->icon=$name;
//        $upicon->save();
//
//        $dir=public_path().'/img/home';
//        $request->file0->move($dir,$name);
////        $books='';
////        return view('linfeng.home.userIcon',compact('books'));
//    }

//我的订单
    public function order()
    {
        $read=readuser::find(session('readid'));
        $books='';
        $orders=order::where('readid',session('readid'))->paginate(4);
        return view('linfeng.home.order',compact('books','read','orders'));
    }

    //充值中心
    public function recharge()
    {
        $collers='';
        $read=readuser::find(session('readid'));
        $books='';
        return view('linfeng.home.recharge',compact('books','read','collers'));
    }

    //充值
    public function money(Request $request)
    {
        $type=$request->type;

        if (!empty($request->sum)){
            $sum=$request->sum;
            $sum=$sum*10;
            $readmony=readuser::find(session('readid'));
            $newrefill=$readmony->refill;
            $readmony->refill=$newrefill+$sum;
            $readmony->save();


            $add=new gomoney();
            $add->readid=session('readid');

            $add->sum=$sum;
            $add->type=$type;
            $add->addtime=time();
            $add->save();
            return 111;
        }
        if (!empty($request->diy)){
            $diy=$request->diy;
            $diy=$diy*10;
            $readmony=readuser::find(session('readid'));
            $newrefill=$readmony->refill;
            $readmony->refill=$newrefill+$diy;
            $readmony->save();

            $add=new gomoney();
            $add->readid=session('readid');
            $add->sum=$diy;
            $add->type=$type;
            $add->addtime=time();
            $add->save();
            return 111;
        }

        return 222;


    }


    //完善信息
    public function upread(Request $request)
    {
            $upread=readuser::find(session('readid'));
            $upread->desc=$request->desc;
            $upread->sex=$request->sex;
            $upread->birthday=$request->birthday;
            $upread->live=$request->prov.$request->city.$request->area.$request->street;
            $upread->save();
            if ($upread){
                return redirect('home/perfect')->with('upread','完善数据成功');
            }else{
                return redirect('home/perfect')->with('upread','完善数据失败,请重新填写');

            }

    }

    //浏览记录
    public function records(){
        //使用多对多模型查询

        $collers=readuser::find(session('readid'))->recordbooks()->orderby('records.addtime','desc')->paginate(8);
//        dd($collers);
        $read=readuser::find(session('readid'));
        $books='';
        return view('linfeng.home.records',compact('books','read','collers'));
    }

    //取消收藏

    public function uncollers(Request $request)
    {
        $del=coller::where('readid',session('readid'))->where('bid',$request->bid);
        $new=$del->delete();
        if ($new){
            return 1;
        }
    }


    //个人详情
    public function means()
    {


        $read=readuser::find(session('readid'));
        $books='';
        $result = readuser::where('id',session('readid'))->get();
        return view('linfeng.home.means',compact('books','read','result'));
    }

    //意见反馈
    public function feedback(Request $request)
    {
        if ($request->isMethod('post')){
            $add=new feedback();
            $add->readid=session('readid');
            $add->up=$request->up;
            $add->num=$request->num;
            $add->addtime=time();
            $add->save();

            $feebackgo=1;
//           $read=readuser::find(session('readid'));

            $request->session()->flash('feebackgo', '1');
            return back();
//        return view('linfeng.home.feedback',compact('read','feebackgo'));
        }
        $aa=DB::table('feedbacks')->where('readid',session('readid'))->get();
        $read=readuser::find(session('readid'));
        return view('linfeng.home.feedback',compact('read','aa'));
    }

    //查询意见反馈ajax
    public function ajaxfeedback()
    {
//        $aa=feedback::where('readid',session('readid'))->get();
        $aa=DB::table('feedbacks')->where('readid',session('readid'))->get();
        return json_encode($aa);
    }

    //查看后台反馈ajax
    public  function kkkfeed(Request $request)
    {
       $id=$request->id;
       $add=tofeedback::where('fid',$id)->get();
       foreach ($add as $k){
           $a=$k->tonum;
       }
       return $a;

    }

}
